package com.situ.student.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.jws.soap.SOAPBinding.Use;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.buf.StringCache;

import com.situ.student.entity.User;
import com.situ.student.service.IUserService;
import com.situ.student.service.impl.UserServiceImpl;
import com.situ.student.util.MD5Util;

public class LoginController extends HttpServlet {
	private IUserService useService = new UserServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		switch (method) {
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req,resp);
			break;
		case "insert":
			insert(req,resp);
			break;	
		default:
			break;
		}
		
	}
	
	//注册
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		User user = new User(name, password);
		
		boolean result = useService.insert(user);
		System.out.println(result ? "成功" : "失败");
		
		resp.sendRedirect(req.getContentType() + "/login.jsp");
	}

	//注销
	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if (null != user) {
			//销毁了
			session.invalidate();
			
			//Listener监听销毁
			//首先从ServletContext取出List<user> onLineUserList
			ServletContext servletContext = getServletContext();
			List<User> onLineUserList = (List<User>) servletContext.getAttribute("onLineUserList");
			onLineUserList.remove(user);
			servletContext.setAttribute("onLineUserList", onLineUserList);
			
			//重定向到登陆页面
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		}
		
	}

	//登陆  synchronized线程同时只能有一个
	private synchronized void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//先验证验证码，先获得用户的验证码
		String checkCode = req.getParameter("checkCode");
		//又获得生成的验证码
		String checkCodeSession = (String) req.getSession().getAttribute("checkCodeSession");
		//判断浏览器输入的值不为空，不为null，不等于生成的值，则重定向到登陆界面
		if (null == checkCode || "" .equals(checkCode) || !checkCode.equalsIgnoreCase(checkCodeSession)) {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
			return;
		}
		
		//得到浏览器的信息
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		//业务处理
		User user = useService.login(name,password);
		//重定向
		
		if (user.getName().equals(name) && user.getPassword().equals(MD5Util.EncodeUtf8(password))){
			//登陆成功
			//创建Session
			HttpSession session = req.getSession();
			session.setAttribute("user", user);
			
			////Listener监听创建
			//首先从ServletContext取出List<user> onLineUserList
			ServletContext servletContext = getServletContext();
			List<User> onLineUserList = (List<User>) servletContext.getAttribute("onLineUserList");
			if (onLineUserList == null) {
				onLineUserList = new ArrayList<>();
			}
			onLineUserList.add(user);
			servletContext.setAttribute("onLineUserList", onLineUserList);
			
			//返回学生界面
			resp.sendRedirect(req.getContextPath()+"/student?method=pageList");
		}else {
			resp.sendRedirect(req.getContextPath()+"/login_again.html");
		}
	}
}
